<%@ include file="../import_page.jsp"%>
<%
//login checking
if(!loginedChecking(session, UsersType.staff)) {
	//user don't login
	//redirect to home page
	response.sendRedirect(URL_HOMEPAGE);
	return;
}

//select unit check
if(!selectUnitCheck(session)) {
	//if user didn't select an Unit
	//redirtect to select page
	response.sendRedirect(URL_SELECT_UNIT);
	return;
}

//input parameter
int acId = -1;
String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
if(isEmptyString(acIdStr)) {
	response.sendRedirect(URL_VIEW_ASSIGNMENT);
	return;
}
//Convert string to int
try {
	acId = Integer.parseInt(acIdStr);
	if(acId < 1)
		throw new Exception();
} catch(Exception err) {
	//incorrect input, go back view submited assignment
	response.sendRedirect(addParmeterToUrl(URL_VIEW_SUBMITED, PAR_ASSIGNMENT_ID, acIdStr));
	return;
}

//input parameter
Users submitBy = null;
String submitByStr = request.getParameter(PAR_SUBMIT_BY);
if(isEmptyString(submitByStr) || submitByStr.length() > Users.ID_MAX_LENGTH) {
	response.sendRedirect(URL_VIEW_ASSIGNMENT);
	return;
}
//get User info of submit By
try {
	UserControl uCon = new UserControl();
	submitBy = uCon.getUser(submitByStr);
	uCon.closeDBConnection();
} catch(Exception err) {
	//incorrect input, go back view submited assignment
	response.sendRedirect(addParmeterToUrl(URL_VIEW_SUBMITED, PAR_ASSIGNMENT_ID, acIdStr));
	return;
}

//get info from database
AssignmentCollector ac = null;
SubmitAssignment sa = null;
ArrayList<MarkingScheme> listMs = null;

ResultControl rCon = new ResultControl();
SubmitAssignmentControl saCon = new SubmitAssignmentControl();

String errMsg = null;
try {
	AssignmentsControl assCon = new AssignmentsControl();
	
	ac = assCon.getAssignment(acId);
	sa = saCon.getSubmited(acId, submitBy.getId());
	listMs = rCon.getMarkingScheme(acId);
	
	assCon.closeDBConnection();
	
} catch(Exception err) {
	errMsg = err.toString();	
}

ArrayList<DetailResult> listDr = new ArrayList<DetailResult>();

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Confirm Marking</title>
<style type="text/css">
<!--
div.MsoNormal {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

li.MsoNormal {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

p.MsoNormal {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

p.MsoNormal1 {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

p.MsoNormal11 {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}
-->
</style>
</head>
<body>
<body>
	<%
if(isEmptyString(errMsg)) {
	boolean inputErr = false;
%>
	<table width="100%" border="1">
		<tr>
			<td width="40%"><strong>Assignment Name</strong>
			</td>
			<td><%= ac.getName() %></td>
		</tr>
		<tr>
			<td><strong>Student Name (ID) </strong></td>
			<td><%= submitBy.getName() %> (<%=submitBy.getId() %>)</td>
		</tr>

		<tr>
			<td><strong>Submit Time (Deadline)</strong>
			</td>
			<td><%= formatDateTime(sa.getSubmitTime()) %> (<%= formatDateTime(ac.getDeadline()) %>)</td>
		</tr>
		<tr>
			<td><strong>File</strong>
			</td>
			<%
Hashtable<String, String> downloadKey = new Hashtable<String, String>(2);
downloadKey.put(PAR_SUBMIT_BY, submitBy.getId());
downloadKey.put(PAR_ASSIGNMENT_ID, Integer.toString(ac.getId()));
%>
			<td><a
				href="<%= addParmeterToUrl(URL_DOWNLOAD_ASSIGNMENT, downloadKey) %>"><%=sa.getFileName() %></a>
			</td>
		</tr>
		<%
//Start to get mark & feedback
BigDecimal totalMark = BigDecimal.ZERO;
Iterator<MarkingScheme> lmsI = listMs.iterator();
while(lmsI.hasNext()) {
	MarkingScheme ms = lmsI.next();
	int msOrder = ms.getOrder();
	//get parameter from request
	String feedback = request.getParameter(PAR_FEEDBACK+ msOrder);
	String markStr = request.getParameter(PAR_MARK+ msOrder);
	BigDecimal mark = null;
	try {
		mark = new BigDecimal(markStr);
		totalMark = totalMark.add(mark);
	} catch (Exception err) {
		inputErr = true;
	}
	if(isEmptyString(feedback))
		feedback = "(None)";
	//if no error, add the marking info to the list
	if(!inputErr) {
		DetailResult dr = new DetailResult(acId, submitBy.getId(), msOrder);
		dr.setFeedback(feedback);
		dr.setMark(mark);
		listDr.add(dr);
	}
	
%>
		<tr>
			<td rowspan="2"><p class="MsoNormal1"
					style='margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; mso-element: frame; mso-element-frame-hspace: 9.0pt; mso-element-wrap: around; mso-element-anchor-horizontal: margin; mso-element-top: 35.15pt; mso-height-rule: exactly'>
					<strong>Criteria <%= msOrder %>: <%=ms.getName() %></strong>
				</p>
				<p class="MsoNormal1"
					style='margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; mso-element: frame; mso-element-frame-hspace: 9.0pt; mso-element-wrap: around; mso-element-anchor-horizontal: margin; mso-element-top: 35.15pt; mso-height-rule: exactly'>
					<strong>Detail:</strong>
				</p>
				<p class="MsoNormal1"
					style='margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; mso-element: frame; mso-element-frame-hspace: 9.0pt; mso-element-wrap: around; mso-element-anchor-horizontal: margin; mso-element-top: 35.15pt; mso-height-rule: exactly'>
					<strong><p><%= muiliteLineString(ms.getCriteria()) %></p>
					</strong>
				</p></td>
			<td>Mark:&nbsp;<%=(mark == null) ? "Unable to Read the mark" : mark %>
				/ <%= ms.getMark() %></td>
		</tr>
		<tr>
			<td>Feedback:<br /> <textarea " rows="5" cols="50"
					readonly="readonly"><%= feedback %></textarea></td>
		</tr>
		<%}%>
		<tr>
			<td>Total Mark</td>
			<td><%= totalMark %> / <%= ac.getFullMark() %></td>
		</tr>
		<tr>
			<td colspan="2">
				<%if(inputErr) {%> Incorrect mark input. Please marking it again. <a
				href="javascript: history.go(-1);">Return</a> <%} else { 
	//checked no error found, save the marking info
	try {
		rCon.markAssignment(ac.getId(), submitBy.getId(), listDr);
		saCon.markStatus(ac.getId(), submitBy.getId(), SubmitStatus.marked);
%> The Marking has been saved. <a href="<%=URL_VIEW_SUBMITED %>">Return
					to View Submited Assignment</a> <%
	} catch (Exception err) {
		out.print(err.toString());
	}
} 
%>
			</td>
		</tr>

	</table>
	<%
} else {
%>
	<p class="MsoNormal" style='tab-stops: 119.7pt'><%= errMsg %></p>
	<%}
//close the result control
saCon.closeDBConnection();
rCon.closeDBConnection();
%>
</body>
</html>